package com.jack.vipmis.vip.card.service;


import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.jack.vipmis.common.entity.search.SearchOperator;
import com.jack.vipmis.common.entity.search.SearchRequest;
import com.jack.vipmis.common.entity.search.Searchable;
import com.jack.vipmis.common.service.BaseService;
import com.jack.vipmis.vip.card.entity.Card;
import com.jack.vipmis.vip.card.repository.CardRepository;

/**
 * 
 * @author JackLiang
 * @date 2014-7-2
 * @version 1.0
 */
@Service
public class CardService extends BaseService<Card, Long> {

	private CardRepository getCardRepository() {
		return (CardRepository) baseRepository;
	}

	/**
	 * 分页查询
	 * 
	 * @param page
	 * @param pageSize
	 * @param cnd
	 *            条件 0默认1是坐标2是下次次数3是最新加载
	 * @param params
	 *            其他的查询条件
	 * @param x
	 * @param y
	 * @return
	 */
	public Page<Card> getCardByPage(int page, int pageSize, String cnd, //
			long cardClass, String type, String x, String y, String text) {
		Searchable searchable = buildSearChable(page, pageSize, cnd, cardClass, type, x, y, text);
		return getCardRepository().findAll(searchable);
	}

	/**
	 * 构建分页对象
	 * 
	 * @param page
	 * @param pageSize
	 * @param sortType
	 * @return
	 */
	public Searchable buildSearChable(int page, int pageSize, String sortType,//
			long cardClass, String type, String x, String y, String text) {
		Searchable searchRequest = SearchRequest.newSearchable();
		if (cardClass > 0) {
			searchRequest.addSearchFilter("cardTaste.id", SearchOperator.eq, cardClass);
		}
		if (type.length() != 0 && !type.equals("0")) {
			searchRequest.addSearchFilter("cardType", SearchOperator.eq, type);
		}

		// 根据距离
		if (StringUtils.isNotBlank(sortType) && sortType.equals("1")) {
			// x
			searchRequest.addSearchFilter("distanceX", SearchOperator.gte,
					Double.parseDouble(StringUtils.isNotBlank(x) ? x : "0") - 0.01);

			searchRequest.addSearchFilter("distanceX", SearchOperator.lte,
					Double.parseDouble(StringUtils.isNotBlank(x) ? x : "0") + 0.01);
			// y
			searchRequest.addSearchFilter("distanceY", SearchOperator.gte,
					Double.parseDouble(StringUtils.isNotBlank(y) ? y : "0") - 0.01);

			searchRequest.addSearchFilter("distanceY", SearchOperator.lte,
					Double.parseDouble(StringUtils.isNotBlank(y) ? y : "0") + 0.01);
		}
		// 根据下载次数
		if (StringUtils.isNotBlank(sortType) && sortType.equals("2")) {
			searchRequest.addSort(Direction.DESC, "cardDownLoadCount");
		}
		// 根据创建时间
		if (StringUtils.isNotBlank(sortType) && sortType.equals("3")) {
			searchRequest.addSort(Direction.DESC, "cardCreateTime");
		}
		// 根据名称来模糊查询
		if (StringUtils.isNotBlank(sortType) && sortType.equals("4")) {
			searchRequest.addSearchFilter("cardName", SearchOperator.like, text);
		}

		searchRequest.setPage((page - 1), pageSize);

		return searchRequest;
	}

}
